package com.seaboxdata.jt809.protocol.decoder;

import com.seaboxdata.jt809.common.Const;
import com.seaboxdata.jt809.common.util.PacketDecoderUtils;
import com.seaboxdata.jt809.packet.JT809BasePacket;
import com.seaboxdata.jt809.packet.JT809Packet0x1402;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;

import static com.seaboxdata.jt809.common.util.CommonUtils.PACKET_CACHE;

/**
 * @ClassName JT809Packet0x1400Decoder
 * @Description TODO
 * @Author DFJX
 * @Company 北京东方金信科技有限公司
 * @Date 2020/7/11 11:38
 * Version 1.0
 **/
public class JT809Packet0x1402Decoder implements Decoder, Serializable {
    private static Logger log = LoggerFactory.getLogger(JT809Packet0x1402Decoder.class);
    @Override
    public JT809BasePacket decoder(byte[] bytes) throws Exception {
        JT809Packet0x1402 jt809Packet0x1402 = new JT809Packet0x1402();
        ByteBuf byteBuf = PacketDecoderUtils.baseDecoder(bytes, jt809Packet0x1402);
        packetDecoder(byteBuf,jt809Packet0x1402);
        return jt809Packet0x1402;
    }
    public void packetDecoder(ByteBuf byteBuf,JT809Packet0x1402 packet){
        ByteBuf msgBodyBuf = null;
        if (packet.getEncryptFlag() == Const.EncryptFlag.NO) {
            msgBodyBuf = PacketDecoderUtils.getMsgBodyBuf(byteBuf);
        } else {
            log.error("报警信息报文已加密！未处理。报文信息：{}",PACKET_CACHE.get(Thread.currentThread().getName()));
            msgBodyBuf = null;
            return;
        }
        // 车牌号
        byte [] vehicleNoBytes = new byte[21];
        msgBodyBuf.readBytes(vehicleNoBytes);
        packet.setVehicleNo(new String(vehicleNoBytes, Charset.forName("GBK")));
        // 车辆颜色
        packet.setVehicleColor(msgBodyBuf.readByte());
        // 子业务类型标识
        packet.setDataType(msgBodyBuf.readShort());
        // 如果报警的数据，抛出空指针错误,解码适配器会对空指针错误做处理。
        if (packet.getDataType() != Const.SubBusinessDataType.UP_WARN_MSG_ADPT_INFO ) {
            throw new NullPointerException();
        }
        // 后续数据长度
        packet.setDataLength(msgBodyBuf.readInt());
        //报警信息来源
        packet.setWarnSrc(msgBodyBuf.readByte());
        //报警类型
        packet.setWarnType(msgBodyBuf.readShort());
        //读取时间
        long longDate=msgBodyBuf.readLong();
        long utc=longDate*1000;
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(utc);
        LocalDate localDate=LocalDate.of(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.DAY_OF_MONTH));
        LocalTime localTime=LocalTime.of(calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),calendar.get(Calendar.SECOND));
        //报警时间
        packet.setWarnTime(LocalDateTime.of(localDate,localTime));
        //信息Id
        packet.setInfoId(msgBodyBuf.readInt());
        //报警信息数据长度,不超过1024字节
        int infoLength=msgBodyBuf.readInt();
        packet.setInfoLength(infoLength);
        //报警信息读取，根据报警信息数据长度读取
        byte[] infoContentBytes=new byte[infoLength];
        msgBodyBuf.readBytes(infoContentBytes);
        packet.setInfoContent(new String(infoContentBytes, Charset.forName("GBK")));
    }
}
